package com.ssy.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.bus.mapper.DepartmentMapper;
import com.ssy.bus.mapper.OutpatientMapper;
import com.ssy.bus.mapper.PatientMapper;
import com.ssy.bus.pojo.Department;
import com.ssy.bus.pojo.Outpatient;
import com.ssy.bus.pojo.Patient;
import com.ssy.bus.service.IOutpatientService;
import com.ssy.bus.vo.OutpatientVo;
import com.ssy.common.CodeMsg;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ssy
 */
@Service
public class OutpatientServiceImpl extends ServiceImpl<OutpatientMapper, Outpatient> implements IOutpatientService {

    @Autowired
    private OutpatientMapper outpatientMapper;

    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public PageResult selectOutpatientByPage(OutpatientVo outpatientVo) {
        Page<Outpatient> page = new Page<>(outpatientVo.getPage(), outpatientVo.getLimit());

        if (StringUtils.hasText(outpatientVo.getPatientName())) {
            Patient patient = patientMapper.selectOne(new LambdaQueryWrapper<Patient>()
                    .select(Patient::getIdNumber)
                    .eq(Patient::getName, outpatientVo.getPatientName()));
            if (patient == null) {
                outpatientVo.setIdNumber("-1");
            } else {
                outpatientVo.setIdNumber(patient.getIdNumber());
            }


        }

        IPage<Outpatient> outpatientIPage = this.outpatientMapper.selectPage(page, new LambdaQueryWrapper<Outpatient>()
                .eq(outpatientVo.getDepId() != null, Outpatient::getDepId, outpatientVo.getDepId())
                .eq(StringUtils.hasText(outpatientVo.getIdNumber()), Outpatient::getIdNumber, outpatientVo.getIdNumber())
                .eq(outpatientVo.getType() != null, Outpatient::getType, outpatientVo.getType())
                .eq(outpatientVo.getIntervals() != null, Outpatient::getIntervals, outpatientVo.getIntervals())
                .eq(outpatientVo.getState() != null, Outpatient::getState, outpatientVo.getState())
                .orderByDesc(Outpatient::getSeeTime));

        List<Outpatient> outpatients = outpatientIPage.getRecords();
        for (Outpatient outpatient : outpatients) {
            String idNumber = outpatient.getIdNumber();
            Patient patient = patientMapper.selectOne(new LambdaQueryWrapper<Patient>()
                    .eq(Patient::getIdNumber, idNumber));
            outpatient.setPatientName(patient.getName());

            Integer depId = outpatient.getDepId();
            Department department = departmentMapper.selectById(depId);
            outpatient.setDepName(department.getName());
            outpatient.setDoctorName(department.getResponsible());

        }
        return new PageResult(outpatientIPage.getTotal(), outpatients);
    }

    @Override
    public Result insertOutpatient(Outpatient outpatient) {
        if (outpatient == null) {
            new Result(CodeMsg.OUTPATIENT_ERROR);
        }
        //Patient patient = this.patientMapper.selectOne(new LambdaQueryWrapper<Patient>().eq(Patient::getIdNumber, outpatient.getIdNumber()));
        //int i;
        //if (patient != null) {
        //    i = this.outpatientMapper.update(outpatient,new LambdaQueryWrapper<Outpatient>().eq(Outpatient::getIdNumber,outpatient.getIdNumber()));
        //} else {
        //
        //}
        int i = this.outpatientMapper.insert(outpatient);
        //修改科室当前挂号量
        departmentMapper.updateDepartmentByNumber(outpatient.getDepId());
        return i > 0 ? new Result(CodeMsg.OUTPATIENT_SUCCESS) : new Result(CodeMsg.OUTPATIENT_ERROR);
    }

    @Override
    public Result selectSeeDoctorPageByDepId(Integer depId) {
        List<Outpatient> outpatients = this.outpatientMapper.selectList(new LambdaQueryWrapper<Outpatient>()
                .eq(depId != null, Outpatient::getDepId, depId)
                .eq(Outpatient::getState, 1));
        for (Outpatient outpatient : outpatients) {
            String idNumber = outpatient.getIdNumber();
            Patient patient = patientMapper.selectOne(new LambdaQueryWrapper<Patient>()
                    .select(Patient::getName).
                    eq(Patient::getIdNumber, idNumber));
            outpatient.setPatientName(patient.getName());

        }
        return new Result(outpatients);
    }

    @Override
    public Result selectOutpatientByNumber(String number) {
        Outpatient outpatient = this.outpatientMapper.selectOne(new LambdaQueryWrapper<Outpatient>()
                .select(Outpatient::getId, Outpatient::getIdNumber)
                .eq(number != null, Outpatient::getNumber, number));
        Patient patient = patientMapper.selectOne(new LambdaQueryWrapper<Patient>().eq(Patient::getIdNumber, outpatient.getIdNumber()));
        outpatient.setPatient(patient);
        return new Result(outpatient);
    }
}
